﻿using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.ParameterSettings
{
    public partial class SetFundType : BaseUI.BasePage
    {
        private BaseBLL baseBLL = new BaseBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindType();
                //初始化绑定数据
                BindProjectName(false);
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindType();
        }

        /// <summary>
        ///
        /// </summary>
        public void BindType()
        {
            string fundName = this.txtTypeName.Text.Trim().ToString();

            string sql = string.Format(@"select typeid 编号, typename 类别名称, remark 备注 from fundtype where 1=1 ");

            if (fundName != "")
            {
                sql += " and typename like '%" + fundName + "%' ";
            }
            DataTable dt = baseBLL.Query(sql);
            gvdata.DataSource = dt;
            gvdata.DataBind();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string fundName = this.txtTypeName.Text.Trim().ToString();
            if (fundName == "")
            {
                Response.Write("<script>alert('类别名称不能为空！');</script>");
                return;
            }
            string sqls = string.Format(@"select * from fundtype where typename='" + fundName + "'");
            int cot = baseBLL.SqlCount(sqls);

            if (cot > 0)
            {
                Response.Write("<script>alert('不能添加重复的用资类别名称！');</script>");
                return;
            }
            string sql = string.Format(@"insert into fundtype(typeid, typename, remark)
        values(typeid.nextval, '" + fundName + @"', '" + fundName + "')");

            int count = baseBLL.NoQuery(sql, CurrentUserName);
            if (count > 0)
            {
                Response.Write("<script>alert('操作成功！');</script>");
                this.txtTypeName.Text = "";
                BindType();
                return;
            }
            else
            {
                Response.Write("<script>alert('操作失败！)');</script>");
                return;
            }
        }

        /// <summary>
        /// 标题排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvdata_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            BindProjectName(true);
        }

        /// <summary>
        /// 绑定项目名称（非动态）绑定GridView
        /// </summary>
        public void BindProjectName(bool iSsort)
        {
            string typeId = this.hidCompanyId.Value.ToString() == "" ? "" : this.hidCompanyId.Value.ToString();

            string sql = "";
            if (typeId == "")
            {
                sql = string.Format(@"select typeid 编号, typename 类别名称, remark 备注 from fundtype where 1=1 ");
            }
            else
            {
                sql = string.Format(@"select typeid 编号, typename 类别名称, remark 备注 from fundtype where 1=1 and typeid=" + typeId);
            }
            DataTable dt = baseBLL.Query(sql);

            DataView view = dt.DefaultView;

            if (iSsort)
            {
                string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
                view.Sort = sort;//设置排序
            }

            gvdata.DataSource = view;

            gvdata.DataBind();
        }
    }
}